import { Entity, Column, OneToOne, ManyToOne, PrimaryGeneratedColumn, JoinColumn } from "typeorm";
import { AssetsType } from "./assets-type.entity";
import { ApiProperty, ApiExtraModels, getSchemaPath } from "@nestjs/swagger";
import { IsOptional, IsNumber, IsString, ValidateNested, ValidateIf } from "class-validator";
import { CrudValidationGroups } from "@nestjsx/crud";
import { BaseEntity } from "./base.entity";
import { Village } from "./village.entity";
import { OperatingAssets } from "./operating-assets.entity";
import { NonOperatingAssets } from "./non-operating-assets.entity";
import { Group } from "./group.entity";

const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class Assets extends BaseEntity {
  @ApiProperty({
    description: '资产主键',
    required: false
  })
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => Village)
      }
    ]
  })
  @ManyToOne(() => Village, Village => Village.assets, {
    eager: true
  })
  village ?: Number | Village

  @ApiProperty({
    example: 1,
    description: '所属组',
    required: true,
  })
  @ManyToOne(
    () => Group,
    Group => Group.assets,
    {
      eager: true,
    },
  )
  group: Number | Group;
  
  
  @ApiProperty({
    example: '资产名字',
    description: '资产名字',
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  name: string

  @ApiProperty({
    example: 1,
    description: '资产类型',
    type: Number
  })
  @ManyToOne(() => AssetsType, AssetsType => AssetsType.assets, {
    eager: true,
  })
  type: number | AssetsType

  @ApiProperty({
    example: 1,
    description: '经营性资产',
    required: false,
    type: Number
  })
  @IsOptional()
  @ValidateIf(o => (typeof o) === "number")
  @ValidateNested()
  @OneToOne(() => OperatingAssets, OperatingAssets => OperatingAssets.assets, {
    cascade: true,
  })
  @JoinColumn()
  operatingAssets ?: OperatingAssets

  @ApiProperty({
    example: 1,
    description: '非经营性资产',
    required: false,
    type: Number
  })
  @IsOptional()
  @ValidateIf(o => (typeof o) === "number")
  @ValidateNested()
  @OneToOne(() => NonOperatingAssets, NonOperatingAssets => NonOperatingAssets.assets, {
    cascade: true,
  })
  @JoinColumn()
  nonOperatingAssets ?: NonOperatingAssets
}